var rowNo ;
var colNo ;
var nowYear ;
var nowMonth ;
var dateFormat = "YYYY-MM-DD" ;
var current_element ;

function getDates(year,month){
    month = month*1+1;
    if(month == 1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12){
        return 31 ;
    }else if( month==4 || month==6 || month==9 || month==11){
        return 30 ;
    }else{
        if(year%400 ==0 || (year%100 !=0 && year%4 == 0)){
            return 29 ;
        }
        return 28 ;
    }
}

function loadMonth(year,month){

    var date = new Date() ;
    date.setYear(year);
    date.setMonth(month);
    date.setDate(1);

    nowYear = year ;
    nowMonth = month ;

    document.getElementById("calendar_year").value = nowYear ;
    document.getElementById("calendar_month").value = nowMonth*1+1 ;

    var day = date.getDay();	//get week day
    var dates = getDates(year,month);

    rowNo = 2;
    colNo = day ;
    var index = 1;

    var matrix = new Array(2,6,7);
	var table_calender = document.getElementById("table_calender");
    for(var t=0;t<matrix.length; t++){
        for(var i=0;i<=6;i++){
            var n = matrix[t];
			if(isMSIE){
				table_calender.rows[n].cells[i].innerText = "" ;
			}else{
				table_calender.rows[n].cells[i].textContent = "" ;
			}
            table_calender.rows[n].cells[i].className = "td_day" ;
        }
    }

	while(index <= dates){
		if(isMSIE){
			table_calender.rows[rowNo].cells[colNo].innerText = index ;
		}else{
	        table_calender.rows[rowNo].cells[colNo].textContent = index ;
		}
		table_calender.rows[rowNo].cells[colNo].className = "td_day" ;
        index++ ;
        getNextTD();
    }
   
}
function getNextTD(){
    if(colNo == 6){
        rowNo++;
        colNo=0 ;
    }else{
        colNo++;
    }
}
function refresh_calendar(me){
    loadMonth(document.getElementById("calendar_year").value,document.getElementById("calendar_month").value);
	me.blur();
	lockCalen = false;
}
function changeLoad(value){
    addMonth(value);
    loadMonth(nowYear,nowMonth);
}

function selectTo(box,val){
    for(var i=0;i<box.options.length;i++){
        if(box.options[i].value*1 == val*1){
            box.options[i].selected = true ;
        }
    }
}
function addYear(offset){
	nowYear = nowYear*1 + offset*1 ;
	loadMonth(nowYear,nowMonth);
}
function addMonth(offset){
    nowMonth = nowMonth*1 + offset*1 ;
    while(nowMonth < 0){
        nowMonth = nowMonth*1 + 12*1 ;
        nowYear-- ;
    }
    while(nowMonth >= 12){
        nowMonth = nowMonth*1 - 12*1 ;
        nowYear++ ;
    }
	loadMonth(nowYear,nowMonth);
}
function setDateFormat(format){
    dateFormat = format ;
}

function getCalenderDate(dvalue){
	document.getElementById("div_calender").style.display = "none";
    current_element.value = dvalue ;
}

function resetClass(){
	var table_calender = document.getElementById("table_calender") ;
    for(var i=2;i<8;i++){
        for(var j=0;j<7;j++){
            table_calender.rows[i].cells[j].className = "td_day";
        }
    }
}
function setDate(me,event){
	if(event == null){
		event = window.event ;
	}

	createCalenderDiv() ;
	if(typeof(me) == 'object'){
		current_element = me ;
	}else{
		current_element = _id(me) ;
	}
	var div_calender = document.getElementById("div_calender") ;

	var x = event.clientX + document.body.scrollLeft + document.documentElement.scrollLeft ;
    var y = event.clientY + document.body.scrollTop + document.documentElement.scrollTop;
	var h = document.body.clientHeight ;
	var w = document.body.clientWidth ;

	if((h - event.clientY) < 162){
		y = y - 160 ;
	}
	if((w - event.clientX) < 356){
		x = x - 240 ;
	}
	var left = 5 ;
	var top = 5 ;
	if((x - 80) > 0 ){
		left = (x - 80) ;
	}
	if((y - 8) > 0 ){
		top = (y - 8) ;
	}
    div_calender.style.left = left + "px";
    div_calender.style.top  = top + "px";
    div_calender.style.display = "block";
	//showObjectProperty(document.body);
}

function setDate1(me){
	createCalenderDiv() ;
    current_element = me ;

    var x = window.event.clientX+document.body.scrollLeft;
    var y = window.event.clientY+document.body.scrollTop;
    div_calender.style.left = x-8;
    div_calender.style.top  = y-8;
	div_calender.style.background = "#CCCCFF";
	div_calender.style.cursor = "crHandPoint";
    div_calender.style.display = "block";
}

var mouseOnCD = false;
var mouseOnCT = false;
var mouseOnCC = false;
var lockCalen = false;

function doOverCD(me){
	mouseOnCD = true ;
}
function doOutCD(me){
	mouseOnCD = false ;
	setTimeout("hiddenCalender()",100);
}
function doOverCT(me){
	mouseOnCT = true ;
}
function doOutCT(me){
	mouseOnCT = false ;
}
function doOverCC(me){
	mouseOnCC = true ;
}
function doOutCC(me){
	mouseOnCC = false ;
}
function do_lockC(flag){
	lockCalen = flag;
}

function createCalenderDiv(){
	var div = _id("div_calender");
	if(div == null){
		div = document.createElement("div");
		div.setAttribute("id","div_calender");
		div.setAttribute("class","div_calendar");
		div.setAttribute("style","display:none;position:absolute;z-index:3;");

		if(isMSIE){
			div.attachEvent("onmouseover",function(){doOverCD(div);});
			div.attachEvent("onmouseout",function(){doOutCD(div);});
			div.style.position = "absolute";
			div.style.zIndex = "30";
		}else{
			div.setAttribute("onmouseover","doOverCD(this)");
			div.setAttribute("onmouseout","doOutCD(this)");
		}

		var f = document.createElement("iframe");
		f.setAttribute("width","366");
		f.setAttribute("height","170");
		f.setAttribute("src","/framework/include/calendar.php");
		f.setAttribute("vspace","0");
		f.setAttribute("hspace","0");
		f.setAttribute("frameborder","0");
		f.setAttribute("scrolling","no");

		if(isMSIE){
			f.style.borderWidth = "0px";
			f.style.borderSpacing = "0px";
			f.style.borderCollapse = "collapse";
			f.frameBorder = "0";
			// showObjectProperty(f.style);
		}

		div.appendChild(f);
		document.body.appendChild(div);
	}
}


function hiddenCalender(){
	var div_calender = document.getElementById("div_calender") ;
	if(!mouseOnCD && !mouseOnCT && !mouseOnCC && !lockCalen){
		div_calender.style.display = "none";
	}
}

function calenderInit(){
    var today = new Date() ;
    nowYear = today.getFullYear();
    nowMonth = today.getMonth();
    writeCalenderTable();
	loadMonth(nowYear,nowMonth);
}


function YHCalendarUpYear(me){
	if(!isInt(me.value)){
		return;
	}
	if(me.value < 1900 || me.value > 3000){
		return ;
	}
	loadMonth(me.value,document.getElementById("calendar_month").value*1-1);
}

function YHCalendarUpMonth(me){
	if(!isInt(me.value)){
		return;
	}
	if(me.value < 1 || me.value > 12){
		return ;
	}
	loadMonth(document.getElementById("calendar_year").value,me.value*1-1);
	
}

function YHCalendarBlurYear(me){
	if(!isInt(me.value)){
		me.value = nowYear ;
	}
	if(me.value < 1900 || me.value > 3000){
		me.value = nowYear ;
	}
}
function YHCalendarBlurMonth(me){
	if(!isInt(me.value)){
		me.value = nowMonth ;
	}
	
}